//
//  YueStatusViewController.m
//  Shengzhuli
//
//  Created by subook on 2017/10/12.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "YueStatusViewController.h"
#import "YueStatusCell.h"
#import "YueKaNetWork.h"
#import "SZLAccountHelper.h"
#import "YueStatusModel.h"
#import "MBProgressHUD+MJ.h"
#import "YueController.h"
#import <MJRefresh.h>

@interface YueStatusViewController () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, assign) NSInteger pageNo;
@property (nonatomic, strong) NSString *type;

@end

@implementation YueStatusViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageNo = 1;
    self.title = @"我与大咖";
    _dataArray = [[NSMutableArray alloc] init];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpTableView];
    [self loadData];
}

- (void)loadData {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"token"] = [SZLAccountHelper shareInstance].token;
    params[@"pageNo"] = @(self.pageNo);
    
    [YueKaNetWork getYueedKaWithParame:params response:^(BOOL success, NSString *msg, NSArray *arr) {
        
        if (success) {
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshingWithNoMoreData];
            for (NSMutableDictionary *dict in arr) {
                YueStatusModel *model = [[YueStatusModel alloc] init];
                [model updateModelWithDictionary:dict];
                [_dataArray addObject:model];
                
            }
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YueStatusModel *model = _dataArray[indexPath.row];
    
    YueStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YueStatusCell class])];
    [cell updateCellWithModel:model];
    
    return cell;
}
- (void)setUpTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, ScreenH-64)) style:(UITableViewStylePlain)];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNo = 1;
        [_dataArray removeAllObjects];
        [self loadData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _pageNo++;
        [self loadData];
    }];
    [self.tableView registerClass:[YueStatusCell class] forCellReuseIdentifier:NSStringFromClass([YueStatusCell class])];
    [self.view addSubview:self.tableView];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YueStatusModel *model = _dataArray[indexPath.row];
    
    YueController *yue = [[YueController alloc] init];
    yue.ID = model.macaId;
    
    [self.navigationController pushViewController:yue animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
